<template>
  <div>
    <el-container>
      <el-aside width="50vw"></el-aside>
      <el-container>
        <!-- <el-header>商品管理系统</el-header> -->
        <el-main>
          <h1>登录</h1>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm form"
          >
            <el-form-item prop="email">
              <el-input
                v-model="ruleForm.email"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item prop="pass">
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <p>
              还没有账号？<router-link to="/register"
                ><span style="color: red">去注册</span></router-link
              >
            </p>
            <el-form-item prop="slide">
              <slide-verify
                ref="slideblock"
                :w="350"
                :h="170"
                @again="onAgain"
                @fulfilled="onFulfilled"
                @success="onSuccess"
                @fail="onFail"
                @refresh="onRefresh"
                :slider-text="text"
                :accuracy="accuracy"
              ></slide-verify>
              <div>{{ msg }}</div>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="submitForm('ruleForm')"
                :disabled="bool"
                >提交</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      let reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!reg.test(value)) {
        callback(
          new Error(
            "请输入6-20个字符，至少一个大写字母，一个小写字母，一个数字"
          )
        );
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      let reg = /^\w+@[a-zA-Z0-9]+((\.[a-z0-9A-Z]{1,})+)$/;
      if (value === "") {
        callback(new Error("请输入邮箱地址"));
      } else if (!reg.test(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        email: "",
      },
      rules: {
        email: [{ required: true, validator: validateEmail, trigger: "blur" }],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
      },
      msg: "",
      text: "向右滑动",
      // 精确度小，可允许的误差范围小；为1时，则表示滑块要与凹槽完全重叠，才能验证成功。默认值为5
      accuracy: 5,
      bool: true,
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios({
            method: "post",
            url: "http://localhost:3000/login",
            data: {
              username: this.ruleForm.email,
              password: this.ruleForm.pass,
            },
          }).then((res) => {
            console.log(res.data);
            let d = new Date();
            d.setTime(d.getTime() + 24 * 60 * 60 * 1000);
            for (let item in res.data.tokenObj) {
              let expires = "expires=" + d.toGMTString();
              document.cookie =
                item + "=" + res.data.tokenObj[item] + "; " + expires;
            }
            if (res.data.status == 405) {
              this.$message({
                showClose: true,
                message: "登陆成功",
                type: "success",
              });
              this.$router.push("/index");
            } else {
              this.$message({
                showClose: true,
                message: "用户名或密码错误",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    onSuccess(times) {
      this.msg = `验证通过, 耗时${(times / 1000).toFixed(1)}s`;
      this.bool = false;
    },
    onFail() {
      this.msg = "验证失败，请重新验证";
      this.bool = true;
    },
    onRefresh() {
      this.msg = "点击了刷新小图标";
    },
    onFulfilled() {
      console.log("刷新成功啦！");
    },
    onAgain() {
      this.msg = "检测到非人为操作的哦！";
      // 刷新
      this.$refs.slideblock.reset();
    },
  },
};
</script>

<style lang="less" scoped>
.el-header,
.el-footer {
  color: #728ded;
  // text-align: center;
  line-height: 60px;
  font-size: 20px;
}

.el-aside {
  background: url("../assets/register.jpg") no-repeat -330px -200px;
  color: #333;
  text-align: center;
  height: 100vh;
}

.el-main {
  color: #333;
  text-align: center;

  h1 {
    line-height: 80px;
    font-weight: bold;
    font-size: 20px;
    margin-top: 100px;
    margin-right: -50px;
  }
}
.form {
  width: 400px;
  margin: 0 auto;
  /deep/ .el-input__inner {
    background-color: #f2f4fa;
  }
  .btnCode {
    width: 112px;
  }
}
.sendCode {
  display: flex;
  justify-content: space-between;
  .code {
    width: 60%;
  }
}
/deep/ .el-form-item {
  margin-left: -50px;
}
</style>